package org.jsrml.core.purchaser.service;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.jsrml.common.persistence.BaseDao;
import org.jsrml.common.unitofwork.aop.UseUnitOfWork;
import org.jsrml.core.purchaser.command.RemoveCompanyCo;
import org.jsrml.core.purchaser.command.SaveCompanyCo;
import org.jsrml.core.purchaser.entity.Company;
import org.jsrml.core.purchaser.exception.PurchaserException;
import org.jsrml.core.purchaser.qo.CompanyQO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class CompanyService extends BaseDao<Company, CompanyQO> {
	
	private static Logger logger = LoggerFactory.getLogger(CompanyService.class);
	
	public Company createCompany(SaveCompanyCo command) throws PurchaserException {
		checkCode(command.getCompanyCode());
		Company company = new Company();
		Company parentCompany = null;
		if(StringUtils.isNotBlank(command.getParentCompanyId())){
			parentCompany=get(command.getParentCompanyId());
		}
		company.create(command,parentCompany);
		save(company);
		return company;
	}

	@UseUnitOfWork
	public Company modifyCompany(SaveCompanyCo command) throws PurchaserException{
		
		Company company = get(command.getCompanyId());

		if (company == null) {
			throw new PurchaserException(
					PurchaserException.COMPANY_NOT_FOUND, "公司没有找到!");
		}
		
		if(!StringUtils.equals(company.getBaseInfo().getCompanyCode(), command.getCompanyCode())){
			checkCode(command.getCompanyCode());
		}

		Company parentCompany = null;
		if(StringUtils.isNotBlank(command.getParentCompanyId())){
			parentCompany=get(command.getParentCompanyId());
		}
		
		company.modify(command,parentCompany);

		return company;
	}
	
	public void removeCompany(RemoveCompanyCo command) throws PurchaserException {
		Company company = get(command.getCompanyId());
		if (company == null) {
			throw new PurchaserException(
					PurchaserException.COMPANY_NOT_FOUND, "公司没有找到!");
		}
		company.remove();
	}
	
	@Transactional(rollbackFor = PurchaserException.class)
	public void plRemoveCompany(RemoveCompanyCo command) throws PurchaserException{
		if(command.getCompanyIds()==null || command.getCompanyIds().size()==0){
			throw new PurchaserException(
					PurchaserException.NECESSARY_PARAM_NULL, "缺少必需的参数!");
		}
		
		for(String companyId : command.getCompanyIds()){
			Company company = get(companyId);
			if (company == null) {
				throw new PurchaserException(
						PurchaserException.COMPANY_NOT_FOUND, "公司没有找到!");
			}
			company.getStatus().setRemove(true);
			save(company);
		}
		
	}
	
	private void checkCode(String code) throws PurchaserException{
		if(StringUtils.isBlank(code)){
			throw new PurchaserException(
					PurchaserException.NECESSARY_PARAM_NULL, "缺少必需的参数!");
		}
		CompanyQO qo = new CompanyQO();
		qo.setCode(code);
		Company company = queryUnique(qo);
		if(company!=null){
			throw new PurchaserException(
					PurchaserException.ENTITY_CODE_FOUND, "公司编码重复!");
		}
	}

	@Override
	protected Criteria buildCriteria(Criteria criteria, CompanyQO qo) {
		return criteria;
	}

	@Override
	protected Class<Company> getEntityClass() {
		return Company.class;
	}

}
